var windowName = "newService";
var Page, LiveList, ProductPics
apiready = function () {
    ProductPics = new PicUp()
    onWindowHide(function () {
    })
    console.log("index page is OK");
    LiveList = new List();
    Page = createVM("#Page", {
        //页面数据
        // lactiveStep:0
        // ,location:location.href
        cats: []
        , cat1: "0"
        , catName1: "家庭保洁"
        , cat2: "0"
        , catName2: "厨房清扫"
        , date: new Date()
        , time: new Date()
        , bookTime: 30
        , title: ""
        , content: ""
        , cur: ''
        , pics: []
        , en: {
            "cat1": 0,
            "cat2": 0,
            "name": "",
            "cover": "",
            "subTitle": "",
            "price": "",
            "unit": "",
            "intro": "",
            "pics": []
        }
    }, {
        //页面方法
        "init": function () {
            var data = getData4Page()

            var id = data.id
            if (isTrue(id)) {
                log("update_")
                getInfo(id)
            }


            // var cats = getLocData("cacheCats")
            this.cats = getLocData("cacheCats");
            var _this = this;
            ProductPics.setCbFunc(function (pics) {
                logJson(pics,"pics")
                _this.pics = ProductPics.getViewData()
                _this.en.pics = ProductPics.getData()
            })
            if (isTrue(getLocData("newServiceName"))) {
                this.en.name = getLocData("newServiceName");
            }
            if (isTrue(getLocData("newServiceIntro"))) {
                this.en.intro = getLocData("newServiceIntro");
            }
        }
        ,setCat1:function (index) {
            this.cat1 = index
            this.en.cat1 = index
        }
        ,setCat2:function (index) {
            this.cat2 = this.cat1 + "_" + index
            this.en.cat2 = index
        }
        ,submit:function () {

            this.$refs.form.validate().then(function (result) {
                if (result) {
                    Page.doSubmit()
                    logJson(result,"test")
                } else {
                    showToast("请检查表单输入是否完整")
                }
            });
        }
        ,doSubmit: function () {
            apiPost("/api/products",Page.en,function (res,code) {
                logJson(res,"res:");
                logJson(code,"code:");
                showToast(res.message);
                goback();
                setLocData("newServiceName","")
                setLocData("newServiceIntro","")
            })

        }
        ,selectPic:function () {
            ProductPics.chooseSrc(9);
        },
        del:function (index) {
            var pics = ProductPics.removePic(index)
            this.pics = ProductPics.getViewData()
            this.en.pics = ProductPics.getData()
        },
        move:function (index,num) {
            var pics = ProductPics.picMove(index,num)
            this.pics = ProductPics.getViewData()
            this.en.pics = ProductPics.getData()
        },
        showPic:function (index,pics) {
            imageBrowser.openImages({
                imageUrls: pics,
                activeIndex:index
            });
        }


    },{
        "en.name":function (val,old) {
            setLocData("newServiceName",val)
        },
    "en.intro":function (val,old) {
            setLocData("newServiceIntro",val)
        }
    })

    Page.init()
}
//获取相机还是相册
function getInfo(id) {
        // "cat1": 0,
        // "cat2": 0,
        // "name": "",
        // "cover": "",
        // "subTitle": "",
        // "price": "",
        // "unit": "",
        // "intro": "",
        // "pics": []
    showLoading();
    apiGet("/api/products/" + id,{},function (res) {
        clearLoading();
        logJson(res,"get_info_:_");
        clearLoading();
        Page.en.id = res.data.id;
        Page.en.cat1 = res.data.cat1;
        Page.en.cat2 = res.data.cat2;
        Page.en.name = res.data.name;
        Page.en.cover = res.data.cover;
        Page.en.subTitle = res.data.subTitle;
        Page.en.price = res.data.price;
        Page.en.unit = res.data.unit;
        Page.en.intro = res.data.intro;
        Page.en.pics = res.data.pics;

        Page.cats.map(function (data,index) {
            // logJson(data)
            if ("/api_/categories/" + data.id == Page.en.cat1) {
                Page.cat1 = data.id
                Page.en.cat1 = data.id
                var sons = data.sons
                // logJson(sons,"=====");
                sons.map(function (d,ind) {
                    // logJson(d)
                    if ("/api_/categories/" + d.id == Page.en.cat2) {
                        Page.cat2 = data.id + "_" + d.id
                        Page.en.cat2 = d.id
                    }
                })
            }

        })

        // Page.cat1 = Page.en.cat1
        // Page.cat2 = Page.en.cat2


        ProductPics.setData(Page.en.pics)
        Page.pics = ProductPics.getViewData(Page.en.pics)
        logJson(Page.pics)
    })
}

